package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.common.utils.SensitiveWordFilter;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.Comment;
import com.example.kaogong.sys.pojo.Post;
import com.example.kaogong.sys.mapper.PostMapper;
import com.example.kaogong.sys.pojo.User;
import com.example.kaogong.sys.pojo.dto.PostDto;
import com.example.kaogong.sys.pojo.vo.PostVo;
import com.example.kaogong.sys.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 帖子 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-04-17
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户发表帖子
     * @param postDto
     * @param name
     * @return
     */
    @Override
    public RespBean addPost(PostDto postDto, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();
        Post post = new Post();
        post.setContent(sensitiveWordFilter.filter(postDto.getContent()));
        post.setPostTime(LocalDateTime.now());
        post.setUid(user.getUid());
        int insert = postMapper.insert(post);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }


    @Override
    public RespBean<Page<PostVo>> getAllPost(Long current, Long size) {
        Page<PostVo> postVoPage = postMapper.getAllPost(new Page<Post>(current, size));
        return RespBean.success(postVoPage);
    }

    @Override
    public RespBean<Page<PostVo>> getCurrentUserPost(Long current, Long size, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();
        Page<PostVo> postVoPage = postMapper.getCurrentUserPost(new Page<Post>(current, size), user.getUid());
        return RespBean.success(postVoPage);
    }

    @Override
    public RespBean deletePostById(Integer postId) {
        int delete = postMapper.delete(new QueryWrapper<Post>().eq("post_id", postId));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }
}
